const path = require('path');
//写好的读取文件模块
const fileUtils = require('./util/file_utils');
//创建websocket 对象
const WebSocket = require('ws');
const wws = new WebSocket.Server({
    port: 9999
})
//监听事件
module.exports.listen =()=>{
  wws.on('connection', client => {
    console.log('客户连接...')
    client.on('message', async msg => {
        //前端发送回来的数据
        let payload = JSON.parse(msg)
        //console.log("16"+JSON.parse(payload).action)
        if(payload.action === 'getData'){
            //返回每一个模块的数据
            console.log("连接胡武器成功的返回值")
            let filePath='./data/'+payload.chartName+'.json';//需要数据的路径
            let filePath1=path.join(__dirname,filePath);
            const res = await fileUtils.getFileJsonData(filePath1)
            payload.data = res //增加data字段
            client.send(JSON.stringify(payload))
            //themeChange  fullScreen
        }else if(JSON.parse(payload).action === 'fullScreen'){
           // 主题切换, 全屏切换, 进行每个客户端的同步, 收到什么数据就发送什么数据
           wws.clients.forEach(client=>{
            //console.log("*******fullScreen*******");
            //console.log(JSON.parse(msg))
            
            client.send(JSON.parse(msg))
           })
        }else{
            wws.clients.forEach(client=>{
                //console.log("*******themeChange*******");
                console.log(JSON.parse(msg))
                client.send(JSON.parse(msg))
               })
        }
  
    })
})
}